﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;
using Sio.Mdm.Entities;
using Sio.Mdm.Client;
using Sio.Mdm.BF.Idm;
using Sio.Mdm.BF.Sifranti;
using Sio.Mdm.Types.Database;
using Sio.Mdm.Types;
using Sio.Mdm.BF.Ldap;
using System.Collections.Generic;

namespace Sio.Mdm.Web.Organizacija
{
    public partial class StarsiSeznam : Controls.Page
    {
        #region Definitions
        public interface UceciRowEditData
        {
            int IdmStevilka { get; set; }
            int VlogaId { get; set; }
            int Spol { get; set; }
            string Ime { get; set; }
            string Priimek { get; set; }
            string Telefon { get; set; }
            string TelefonSluzba { get; set; }
        }
        #endregion

        #region OnLoad
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack && !X.IsAjaxRequest)
            {
                //spoli
                Dictionary<int, string> listSpoli = new Dictionary<int, string>();
                listSpoli.Add(0, "moški");
                listSpoli.Add(1, "ženski");
                var tmpArray = (from p in listSpoli select new Object[2] { p.Key, p.Value }).ToArray();
                this.StoreSpoli.DataSource = tmpArray;
                this.StoreSpoli.DataBind();
            }
            NaloziSeznamStarsev();
        }
        #endregion

        #region Private
        private void NaloziSeznamStarsev()
        {
            //vhodni podatki
            string RazredID = (Request.GetValue<int>("razredid") > 0) ? Request.GetValue<string>("razredid") : "NULL";
            string Razred = (Request.GetValue<int>("razred") > 0) ? Request.GetValue<string>("razred") : "NULL";
            string OrganizacijaID = "-1";
            string LetoId = Request.GetValue<string>("letoid");

            var filter = from r in new Filter<Stars>()
                         join sp in new Filter<StarsIzRazreda>(RazredID, Razred, OrganizacijaID, LetoId) on r.IdmStevilka equals sp.IdmStevilka
                         where r.OrganizacijaId == this.UserSession.Organizacija
                         select r;
            GridStarsiSeznam.LoadData<Stars>(filter);
        }
        #endregion
        #region Editing
        protected void UrediPodatke(Object sender, DirectEventArgs e)
        {
            UceciRowEditData EditData = XML.Deserialize<UceciRowEditData>(e.ExtraParams["data"]);
            if (EditData.IdmStevilka > 0)
            {
                Stars st = EntityFactory.LoadEntity<Stars>(
                        new Filter<Stars>().Where(r => r.IdmStevilka == EditData.IdmStevilka)
                );

                if (st.EntityState == EntityState.Unchanged)
                {
                    st.Ime = EditData.Ime;
                    st.Priimek = EditData.Priimek;
                    st.Telefon = EditData.Telefon;
                    st.TelefonSluzba = EditData.TelefonSluzba;
                    st.Spol = EditData.Spol;
                    st.Update();
                }
                else
                {
                    e.Success = false;
                    X.Msg.Alert("Manjkajoči podatki", "Iskani zapis ne obstaja.").Show();
                    return;
                }
            }
        }
        protected void OdstraniStarse(object sender, DirectEventArgs e)
        {
            List<int> deletedItems = e.GetIdList<int>("selected", "IdmStevilka");
            EntityCollection<Stars> col = new EntityCollection<Stars>(
                    new Filter<Stars>().SelectByField(r => r.IdmStevilka, deletedItems.ToArray())
            );

            col.RemoveAll();
            col.Update();
        }
        #endregion

    }
}
